package com.firexxx.vp.infrastructure.query.impl;

import com.firexxx.vp.domain.audio.AudioEntity;
import com.firexxx.vp.domain.audio.AudioRepository;
import com.firexxx.vp.domain.audio.query.AudioQueryInterface;
import com.firexxx.vp.domain.stb.StbKey;
import com.firexxx.vp.domain.vp.single.SingleVoicePrintEntity;
import com.firexxx.vp.domain.vp.single.SingleVoicePrintFinishedEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.event.TransactionalEventListener;

import java.io.IOException;
import java.util.List;

/**
 * @description: TODO
 * @throws:
 * @author: zhuangzhang2
 * @date: 2019/12/25 17:22
 */
public class BaseAudioService implements AudioQueryInterface {

    @Autowired
    private AudioRepository audioRepository;

    @Override
    public List<AudioEntity> findAllByStbKey(StbKey stbKey) {
        return audioRepository.findAllByStbKey(stbKey);
    }

    @Override
    public List<AudioEntity> findByIdIn(List<String> idsList) {
        return audioRepository.findByIdIn(idsList);
    }

    @Override
    public AudioEntity findByAudioId(String id) {
        return audioRepository.findByAudioId(id);
    }

    @Async
    @TransactionalEventListener
    public void addAudio(SingleVoicePrintFinishedEvent event) {
        SingleVoicePrintEntity singleVoicePrintEntity = event.getSource();

        audioRepository.save(singleVoicePrintEntity.getAudioEntity());
    }

    @Async
    @TransactionalEventListener
    public void storeAudioFile(SingleVoicePrintFinishedEvent event) throws IOException {
        SingleVoicePrintEntity singleVoicePrintEntity = event.getSource();

        singleVoicePrintEntity.getAudioEntity().store();
    }
}